﻿Ext.ns("Kwan");
Kwan = {
    // helper
    objToString: function (o) {
        var s = '{\n';
        for (var p in o)
            s += '    "<b>' + p + '</b>": "' + o[p] + '"\n';
        return s + '}';
    },
    addTab: function (config) {
        var id = config.id.split('_');
        var path = id.length > 3 ? id[2] + '/' + id[3] : id[2];
        var url = "/Ext/" + path + "/Default.aspx";
        var tabPanel = config.tabPanel
        var tab = tabPanel.getComponent(config.id);
        if (!tab) {
            tab = tabPanel.add({
                id: config.id,
                title: config.title,
                closable: true,
                border: true,
                iconCls: config.icon,
                //tabTip: config.title,
                autoLoad: {
                    showMask: true,
                    url: url,
                    mode: "iframe",
                    //params: {
                    //    param1: "param1",
                    //    param2: "param2"
                    //},
                    maskMsg: "正在加载 " + config.title + "...",
                    //nocache: true, 
                    scripts: true, scope: this
                }
            });

            tab.on("activate", function () {
                var mp = Ext.getCmp('mp_' + id[1]);
                var item = mp.menu.items.get(id[1] + "_" + id[2]);
                if (item) {
                    mp.setSelection(item);
                }
            }, this);
        }
        tabPanel.setActiveTab(tab);
        Ext.get(tab.tabEl).frame();
        //this.Debug(tabPanel.items.items[0].iconUrl);
    },
    selectionChanged: function (dv, nodes) {
        if (nodes.length > 0) {
            Kwan.addTab({ tabPanel: TabPanel1, id: 'tab_' + nodes[0].id, title: nodes[0].title, icon: 'Kwan-icon-' + nodes[0].id.split('_')[1] });
        }
    },
    viewClick: function (dv, e) {
        var group = e.getTarget("h2", 3, true);
        if (group) { group.up("div").toggleClass("collapsed"); }
    }
};